package com.att.action;

import com.att.bean.NumRunDeilBean;
import com.att.bean.SchClassBean;
import com.att.bean.UserTempSchBean;
import com.att.service.NumRunDeilService;
import com.att.service.UserTempSchService;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("userTempSch")
public class UserTempSchAction {

    @Resource(name = "userTempSchService")
    private UserTempSchService userTempSchService;
    @Resource(name = "numRunDeilService")
    private NumRunDeilService numRunDeilService;


    //新增
    @RequestMapping("insert")
    @ResponseBody
    @Transactional
    public String insert(UserTempSchBean bean) throws ParseException {

//        Calendar cal = Calendar.getInstance();
//        long time=bean.getEndTime().getTime()-bean.getStartTime().getTime();       //获取时间差
//        Integer day= Math.toIntExact(time / (24 * 60 * 60 * 1000));
        boolean flag = userTempSchService.insert(bean);
        if (flag) {
            NumRunDeilBean numRunDeilBean = new NumRunDeilBean();
            numRunDeilBean.setStartTimeStr(bean.getStartTimeStr());
            numRunDeilBean.setNumRunId(bean.getNumRunId());
            numRunDeilBean.setSchClassId(bean.getSchClassId());
            numRunDeilBean.setIsAddWork(bean.getIsAddWork());
            numRunDeilBean.setAddWorkTime(bean.getAddWorkTime());
            numRunDeilBean.setStartDays(bean.getStartDays());
            numRunDeilBean.setDeilType(bean.getDeilType());
            boolean flag2 = numRunDeilService.insert(numRunDeilBean);
            if (!flag2) {
                return "ERROR";
            }
//            cal.setTime(bean.getStartTime());
//            for (int i = 0;i<day;i++){
//                cal.add(cal.DAY_OF_MONTH, +1);
//                bean.setStartTime(cal.getTime());
//                bean.setEndTime(cal.getTime());
//
//            }
        }
        return "SUCCESS";
    }


    //取消临时排班
    @RequestMapping("delete")
    @ResponseBody
    @Transactional
    public String delete(UserTempSchBean bean) {
        boolean flag = userTempSchService.delete(bean);
        if (flag) {
            NumRunDeilBean numRunDeilBean = new NumRunDeilBean();
            numRunDeilBean.setNumRunId(bean.getNumRunId());
            boolean flag2 = numRunDeilService.deleteTempDeil(numRunDeilBean);
            if (flag2) {
                return "SUCCESS";
            }
        }
        return "ERROR";
    }
}
